import { createRouter, createWebHashHistory  } from 'vue-router'

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/Home.vue'),
    },
    {
      path: '/recipeManage',
      name: 'RecipeManage',
      component: () => import('../views/RecipeManage.vue'),
    },
    {
      path: '/roleManage',
      name: 'roleManage',
      component: () => import('../views/RoleManage.vue'),
    },
    {
      path: '/logManage',
      name: 'logManage',
      component: () => import('../views/LogManage.vue'),
    },
    {
      path: '/reportManage',
      name: 'reportManage',
      component: () => import('../views/ReportManage.vue'),
    },
    {
      path: '/chartManage',
      name: 'chartManage',
      component: () => import('../views/ChartManage.vue'),
    },
  ],
})

router.beforeEach(async (to, from,next) => {
  await CheckInit();
  next()
});

const CheckInit = ()=>{
  let timer = null;
  return new Promise((resolve)=>{
    if(window.isInitOk){
      resolve();
      return 
    }
    timer = setTimeout(() => {
      CheckInit().then(()=>{
        timer&&clearTimeout(timer);
        resolve();
      })
    }, 50);
  })
}

export default router
